# 链表翻转（反转链表）—— 高频必问！
# 单链表反转

class Node:
    def __init__(self,val):
        self.val = val
        self.next = None


def reverse_linked_list(head):
    prev = None
    curr = head
    while curr:
        next_node = curr.next   # 保存下一个节点
        curr.next = prev        # 反转指针
        prev = curr
        curr = next_node
    return prev     # 新的节点


# 测试
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)

new_head = reverse_linked_list(head)

while new_head:
    print(new_head.val, end='->')
    new_head = new_head.next

# 输出：3->2->1->
